operator new, operator new[]

From Cppreference

< cpp | memory | new
Jump to: navigation, search

Template:cpp/memory/new/sidebar

Defined in header <new>

void* operator new  ( std::size_t count );
(1)
void* operator new[]( std::size_t count );
(2)
void* operator new  ( std::size_t count, const std::nothrow_t& );
(3)
void* operator new[]( std::size_t count, const std::nothrow_t& );
(4)
void* operator new  ( std::size_t, void* ptr );
(5)
void* operator new[]( std::size_t, void* ptr );
(6)

Allocates requested number of bytes. These allocation functions are called by the new expression to allocate memory in which new object would then be initialized. These functions are implicitly declared in each translation unit even if the <new> header is not included. Also, these functions have implicit implementation unless the program provides its own implementation.

1-2) allocate count bytes, throws an std::bad_alloc() on failure

3-4) allocate count bytes, returns NULL on failure

5-6) does nothing, returns ptr. If called through new expression, this version can be used to construct an object in user supplied memory area.

Contents

[edit] Replacing and overloading

The program can implement (1-4) versions of the allocation functions, which would then be used instead of the default implementations. Also, program can add versions of placement allocation functions with different signatures (except that it is not permitted to replace (5-6) versions of the allocation function). The added signature should look like the following, where count is number of bytes to allocate and placement_params are the parameters supplied to the new expression:

void* operator new  (size_t count/*, placement_params*/);
for the new version
void* operator new[](size_t count/*, placement_params*/);
for the new[] version

The allocation function can be replaced/overloaded in two ways:

in the global scope: in order to call it, the signature of the overloaded allocation functions must be visible at the place of allocation, except for implicitly declared default allocation functions. This allocation function will be used for all allocations with corresponding parameters in the current program
in the local scope: the overloaded operator new must be static public member function of the class. This allocation function will be used only for allocations of that particular class.

During compilation, each new expression looks up for appropriate allocation function's name firstly in the class scope and after that in the global scope. It can be instructed to skip the first step by calling new as ::new. Note, that as per overloading rules, any allocation functions declared in class scope hides all global allocation functions. For more information see new expression. Note, that it is not possible to place allocation function in a namespace.

Parameters

count - number of bytes to allocate
ptr - pointer to a memory area to initialize the object at

Return value

1-4) pointer to allocated memory area

5-6) ptr

Exceptions

1-2) throws an std::bad_alloc() on failure to allocate memory

3-6) none

See also


Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox